package serie09;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;


import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

import serie09.SpeedometerModel.SpeedUnit;


public class Speedometer {

	// ATTRIBUTS
	
	// La fenetre principale
	private JFrame frame;
	
	// Le GraphicSpeedometer
	private GraphicSpeedometer g;
	
	// Les boutons de choix de la vitesse
	private JRadioButton km;
	
	private JRadioButton mph;
	
	// Les boutons pour augmenter ou diminuer la vitesse
	private JButton minus;
	private JButton plus;
	
	// Le bouton d'allumage et d'arrêt
	private JButton turn;
	
	// CONSTRUCTEURS
	
	public Speedometer() {
        createView();
        placeComponents();
        createController();
	}
	
	
	/**
     * Rend l'application visible au centre de l'�cran.
     */
    public void display() {
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    // OUTILS
    
    private void createView() {
    	frame = new JFrame("Speedometer");
    	g = new GraphicSpeedometer();
    	km = new JRadioButton("Km/h");
    	mph = new JRadioButton("M.p.H.");
    	if (g.getModel().getUnit() == SpeedUnit.KMH) {
    		km.setSelected(true);
    	} else {
    		mph.setSelected(true);
    	}
    	minus = new JButton("-");
    	minus.setEnabled(false);
    	plus = new JButton("+");
    	plus.setEnabled(false);
    	turn = new JButton("Turn ON");
    	g.getModel().addObserver(new Observer() {
            public void update(Observable o, Object arg) {
            	SpeedometerModel m = (SpeedometerModel) o;
            	if (m.isOn()) {
    				plus.setEnabled(true);
    				minus.setEnabled(true);
    				turn.setText("Turn OFF");
    			} else {
    				plus.setEnabled(false);
    				minus.setEnabled(false);
    				turn.setText("Turn ON");
    			}
            }
    	});
    }
    
    private void placeComponents() { 
    	JPanel p = new JPanel(new GridLayout(0, 1)); {
    		JPanel q = new JPanel(); {
    			JPanel t = new JPanel(new GridLayout(0, 1)); {
    				t.add(km);
    				t.add(mph);
    			}
    			t.setBorder(BorderFactory.createEtchedBorder());
    			q.add(t);
    		}
    		p.add(q);
    		q = new JPanel(); {
    			JPanel t = new JPanel(new GridLayout(1, 0)); {
    				t.add(minus);
    				t.add(plus);
    			}
    			t.setBorder(BorderFactory.createEtchedBorder());
    			q.add(t);
    		}
    		p.add(q);
    		q = new JPanel(); {
    			q.add(turn);
    		}
    		p.add(q);
    	}
    	frame.add(p, BorderLayout.WEST);
    	frame.add(g, BorderLayout.CENTER);
    }
    
    private void createController() {
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	ButtonGroup bg = new ButtonGroup();
    	bg.add(km);
    	bg.add(mph);
    	turn.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			if (turn.getText().equals("Turn ON")) {
    				g.getModel().turnOn();
    			} else {
    				g.getModel().turnOff();
    			}
    		}
    	});
    	km.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			g.getModel().setUnit(SpeedUnit.KMH);
    		}
    	});
    	mph.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			g.getModel().setUnit(SpeedUnit.MPH);
    		}
    	});
    	plus.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			g.getModel().speedUp();
    		}
    	});
    	minus.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			g.getModel().slowDown();
    		}
    	});
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Speedometer().display();
            }
        });
    }

}
